package com.example.myapplication.found.model;

import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockKlineBean;
import com.example.myapplication.found.bean.StockOrderBean;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * @author: rongfeng
 * @date: 2022/3/7
 * @description
 */
public class Point2 extends BaseData {
    List<StockOrderBean> orderList = new ArrayList<>();
    int endDay = 1;

    public Point2(StockBean stockBean, StockBean weekStockBean) {
        super(stockBean, weekStockBean);
        pool = Executors.newFixedThreadPool(1);
    }

    @Override
    public void start() {
        pool.execute(new Runnable() {
            @Override
            public synchronized void run() {
                if (endDay < stockBean.klineList.size() - 1) {
                    endDay++;
                    test1();
                } else {
                    float orderZhang = 0, orderDie = 0, orderPin = 0, orderAvg = 0, money = 0;
                    for (StockOrderBean orderBean : orderList) {
                        if (orderBean.zhang) {
                            orderZhang++;
                        }
                        if (orderBean.die) {
                            orderDie++;
                        }
                        if (orderBean.pin) {
                            orderPin++;
                        }
                        money = money + orderBean.money;
                        orderAvg = orderAvg + orderBean.average;
                    }
                    if (orderList.size() > 0) {
                        System.out.println(fundName + " " + fundCode);
                        System.out.println("买入涨 " + orderZhang + " 跌 " + orderDie + "  平 " + orderPin + " 胜率 " + (orderZhang / orderList.size()) * 100 + "%");
                        System.out.println("平均剩余金额 " + money / orderList.size());
                        System.out.println();
                    }
//                    if (simpleCallback != null) {
//                        simpleCallback.pointCallback(orderList);
//                    }
                }
            }
        });


    }

    private List<StockKlineBean> getMinPointList() {
        List<StockKlineBean> pointList = new ArrayList<>();
        pointList.add(stockBean.klineList.get(0));
        pointList.add(stockBean.klineList.get(1));
        boolean isUP = stockBean.klineList.get(1).chg > 0;
        for (int i = 2; i < endDay; i++) {
            StockKlineBean klineBean = stockBean.klineList.get(i);
            StockKlineBean lastKlineBean = pointList.get(pointList.size() - 1);
            if (klineBean.shouPan > lastKlineBean.shouPan) {
                if (isUP) {
                    pointList.set(pointList.size() - 1, klineBean);
                } else {
                    pointList.add(klineBean);
                }
                isUP = true;
            } else {
                if (isUP) {
                    pointList.add(klineBean);
                } else {
                    pointList.set(pointList.size() - 1, klineBean);
                }
                isUP = false;
            }
        }
        return pointList;
    }

    private void test1() {
        List<StockKlineBean> pointList = getMinPointList();
        StockKlineBean item = stockBean.klineList.get(endDay);
        for (int i = 0; i < pointList.size(); i++) {
            StockKlineBean klineBean2 = pointList.get(i);
            float cValue = (item.shouPan - klineBean2.shouPan) / klineBean2.shouPan * 100f;
            if (item.index > klineBean2.index + 1 && Math.abs(cValue) <= 1) {
                nextChg(item, klineBean2);
                break;
            }
        }
        start();

//        for (int i = 0; i < pointList.size(); i++) {
//            StockKlineBean klineBean = pointList.get(i);
//            for (int j = i + 1; j < pointList.size(); j++) {
//                StockKlineBean klineBean2 = pointList.get(j);
//                float cValue = (klineBean.shouPan - klineBean2.shouPan) / klineBean2.shouPan * 100f;
//                if (Math.abs(cValue) <= 1) {
//                    System.out.println("相似点 " + klineBean.date + " " + klineBean.shouPan + " : " + klineBean2.date + " " + klineBean2.shouPan);
//                    nextChg(klineBean,klineBean2);
//                }
//            }
//        }
    }

    public void nextChg(StockKlineBean lastKlineBean1, StockKlineBean toKlineBean2) {
        float money = 10000;
        for (int i = 0; i < stockBean.klineList.size(); i++) {
            StockKlineBean klineBean3 = stockBean.klineList.get(i);
            if (klineBean3.date.equals(lastKlineBean1.date) && lastKlineBean1.chg < 0 && toKlineBean2.chg < 0) {
                System.out.println(lastKlineBean1.date + " " + lastKlineBean1.shouPan + "  相似点   " + toKlineBean2.date + " " + toKlineBean2.shouPan);
                for (int j = 1; j <= 2; j++) {
                    if (j + i < stockBean.klineList.size()) {
                        StockKlineBean nextItem = stockBean.klineList.get(j + i);
                        money = money + (money * (nextItem.chg / 100));
                        System.out.println(nextItem.date + " " + nextItem.chg + "%");
                    }
                }
                StockOrderBean orderBean = new StockOrderBean();
                orderBean.average = money;
                orderBean.zhang = money > 10000;
                orderBean.die = money < 10000;
                orderBean.pin = money == 10000;
                orderBean.money = money;
                orderList.add(orderBean);
                endDay = endDay + 2;
                break;
            }
        }
    }
}
